package com.jyw.business.comp.service.impl;

import java.util.List;

import com.jyw.business.comp.domain.CompScheduleShiftTime;
import com.jyw.business.comp.domain.dto.ShiftAddDto;
import com.jyw.business.comp.domain.dto.ShiftEditDto;
import com.jyw.business.comp.service.ICompScheduleShiftTimeService;
import org.springframework.stereotype.Service;
import com.jyw.business.comp.mapper.CompScheduleShiftMapper;
import com.jyw.business.comp.domain.CompScheduleShift;
import com.jyw.business.comp.service.ICompScheduleShiftService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 综合-班次Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-25
 */
@Service
public class CompScheduleShiftServiceImpl extends ServiceImpl<CompScheduleShiftMapper,CompScheduleShift> implements ICompScheduleShiftService
{
    @Resource
    private CompScheduleShiftMapper compScheduleShiftMapper;

    @Resource
    private ICompScheduleShiftTimeService shiftTimeService;

    /**
     * 查询综合-班次
     * 
     * @param id 综合-班次主键
     * @return 综合-班次
     */
    @Override
    public CompScheduleShift selectCompScheduleShiftById(Long id)
    {
        return compScheduleShiftMapper.selectCompScheduleShiftById(id);
    }

    /**
     * 查询综合-班次列表
     * 
     * @param compScheduleShift 综合-班次
     * @return 综合-班次
     */
    @Override
    public List<CompScheduleShift> selectCompScheduleShiftList(CompScheduleShift compScheduleShift)
    {
        return compScheduleShiftMapper.selectCompScheduleShiftList(compScheduleShift);
    }

    /**
     * 新增综合-班次
     * 
     * @param compScheduleShift 综合-班次
     * @return 结果
     */
    @Override
    public int insertCompScheduleShift(CompScheduleShift compScheduleShift)
    {
        return compScheduleShiftMapper.insertCompScheduleShift(compScheduleShift);
    }

    /**
     * 修改综合-班次
     * 
     * @param compScheduleShift 综合-班次
     * @return 结果
     */
    @Override
    public int updateCompScheduleShift(CompScheduleShift compScheduleShift)
    {
        return compScheduleShiftMapper.updateCompScheduleShift(compScheduleShift);
    }

    /**
     * 批量删除综合-班次
     * 
     * @param ids 需要删除的综合-班次主键
     * @return 结果
     */
    @Override
    public int deleteCompScheduleShiftByIds(Long[] ids)
    {
        return compScheduleShiftMapper.deleteCompScheduleShiftByIds(ids);
    }

    /**
     * 删除综合-班次信息
     * 
     * @param id 综合-班次主键
     * @return 结果
     */
    @Override
    public int deleteCompScheduleShiftById(Long id)
    {
        return compScheduleShiftMapper.deleteCompScheduleShiftById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ShiftAddDto dto) {
        CompScheduleShift shift = dto.build();
        //保存班次
        save(shift);
        //保存班次时间
        List<CompScheduleShiftTime> compScheduleShiftTimes = dto.buildShiftTime(shift.getId());
        if(!CollectionUtils.isEmpty(compScheduleShiftTimes)){
            shiftTimeService.saveBatch(compScheduleShiftTimes);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(ShiftEditDto dto) {
        CompScheduleShift shift = dto.build();
        //修改班次
        saveOrUpdate(shift);
        //删除班次时间
        shiftTimeService.lambdaUpdate().eq(CompScheduleShiftTime::getShiftId,shift.getId()).remove();
        //重新保存班次时间
        List<CompScheduleShiftTime> compScheduleShiftTimes = dto.buildShiftTime(shift.getId());
        if(!CollectionUtils.isEmpty(compScheduleShiftTimes)){
            shiftTimeService.saveBatch(compScheduleShiftTimes);
        }
    }
}
